<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>主页</title>
    <style>
        /* 页面基本样式 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f9f9f9;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .denglu {
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            text-align: center;
        }

        h2 {
            color: #333;
            font-size: 24px;
            margin-bottom: 20px;
        }

        .input-group {
            margin-bottom: 15px;
            text-align: left;
        }

        .input-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
        }

        .input-group input {
            width: 100%;
            padding: 10px;
            font-size: 14px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
            transition: border 0.3s;
        }

        .input-group input:focus {
            border-color: #4e9bff;
            outline: none;
        }

        .button {
            background-color: #4e9bff;
            color: #fff;
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
            transition: background-color 0.3s;
        }

        .button:hover {
            background-color: #3578e5;
        }

        .button:disabled {
            background-color: #b0d6ff;
            cursor: not-allowed;
        }

        #output {
            color: #e74c3c;
            margin-top: 20px;
        }

        a {
            display: block;
            margin-top: 20px;
            color: #3498db;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <div class="denglu">
        <h2>获取邮箱验证码</h2>
        <form id="dataForm">
            <div class="input-group">
                <label for="email">邮箱 (请使用国内邮箱)</label>
                <input type="text" id="email" name="email" placeholder="请输入邮箱地址">
            </div>

            <div class="input-group">
                <input type="button" value="获取验证码" id="getVerificationCodes" class="button">
            </div>
        </form>

        <pre id="output">注册消息(记住自己邮箱和验证码)</pre>

        <a href="./html/register.html"> 注册页面</a>
        <a href="./html/login.html"> 登录页面</a>
        <a href="./html/chagePass.html"> 修改密码页面</a>
    </div>

    <script>
        let emailTimeOut = 0;

        function isValidEmail(email) {
            const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
            return regex.test(email);
        }

        const getVerificationCodes = document.getElementById('getVerificationCodes');
        getVerificationCodes.addEventListener('click', async function (event) {
            event.preventDefault();  // 阻止表单默认提交

            const email = document.getElementById('email').value;

            try {
                if (emailTimeOut <= 0) {
                    if (!email) {
                        document.getElementById("output").textContent = "邮箱不能为空";
                    } else if (!isValidEmail(email)) {
                        document.getElementById("output").textContent = "邮箱格式不正确";
                    } else {
                        const response = await fetch("http://124.221.110.118:3000/send_verificationCode", {
                            method: "POST",
                            headers: {
                                "Content-Type": "application/json"
                            },
                            body: JSON.stringify({ email: email })
                        });

                        const result = await response.json();

                        if (response.status === 200) {
                            document.getElementById("output").textContent = "验证码已发送，请检查邮箱";
                            emailTimeOut = 60;
                            setTimeout(() => {
                                emailTimeOut = 0;
                                // window.location.href = "./register.html"; // Optionally redirect
                            }, 60000);
                        } else if (response.status === 400) {
                            document.getElementById("output").textContent = "验证码获取时间间隔太短，请稍后再试";
                        } else {
                            document.getElementById("output").textContent = "发送验证码失败，请稍后再试";
                        }
                    }
                } else {
                    document.getElementById("output").textContent = "发送验证码冷却中，请稍后再试";
                }
            } catch (error) {
                console.error("请求失败", error);
                document.getElementById("output").textContent = "网络错误，发送失败！";
            }
        })
    </script>
</body>

</html>
